﻿
using System;
using System.Configuration;
using System.Diagnostics;

namespace MefContrib.Models.Provider.Definitions.Configurable
{
    public class PartCatalogConfigurationCollection<TItemType> : ConfigurationElementCollection where TItemType : ConfigurationElement, new()
    {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string _elementName;

        ///// <summary>
        ///// Initializes a new instance of the <see cref="PartCatalogConfigurationCollection{TItemType}"/>
        ///// </summary>
        //public PartCatalogConfigurationCollection()
        //{
        //}

        /// <summary>
        /// Initializes a new instance of the <see cref="PartCatalogConfigurationCollection{TItemType}"/>
        /// </summary>
        /// <param name="elementKey"></param>
        /// <param name="elementName"></param>
        /// <param name="innerCollection"></param>
        public PartCatalogConfigurationCollection(object innerCollection, string elementName, Func<TItemType, object> elementKey)
        {
            ConfigurationElementCollection collection = innerCollection as ConfigurationElementCollection;
            if (collection == null)
            {
                throw new ArgumentException();
            }

            this.ElementKey = elementKey;
            this._elementName = elementName;
            this.InnerCollection = collection;
        }

        public static PartCatalogConfigurationCollection<TItemType> Create(
            object innerCollection,
            string elementName,
            Func<TItemType, object> elementKey)
        {
            ConfigurationElementCollection collection = innerCollection as ConfigurationElementCollection;
            if (collection == null)
            {
                throw new ArgumentException();
            }
            
            return new PartCatalogConfigurationCollection<TItemType>(collection, elementName, elementKey);       
        }

        public Func<TItemType, object> ElementKey { get; set; }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <value></value>
        public ConfigurationElementCollection InnerCollection { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new TItemType();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <value></value>
        protected override string ElementName
        {
            [DebuggerStepThrough]
            get { return this._elementName; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            TItemType part =
                (TItemType)element;

            return this.ElementKey(part);
        }
    }
}